package com.luyue.zagj.fragment;


import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.luyue.zagj.MyApplication;
import com.luyue.zagj.R;
import com.luyue.zagj.activity.home.VehicleInfoActivity;
import com.luyue.zagj.activity.myAccount.AccountGiftActivity;
import com.luyue.zagj.activity.myAccount.CarInfoActivity;
import com.luyue.zagj.activity.myAccount.RepairRecordActivity;
import com.luyue.zagj.activity.myAccount.ReportRecordActivity;
import com.luyue.zagj.activity.myAccount.SetActivity;
import com.luyue.zagj.activity.myAccount.VehicleManageActivity;
import com.luyue.zagj.activity.myAccount.VersionMsgActivity;
import com.luyue.zagj.adapter.ImagePager;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2016/8/24 0024.
 */
public class MyAccountFragment extends Fragment {
    @Bind(R.id.my_acc_manager_rl)
    LinearLayout myAccManagerRl;
    @Bind(R.id.my_acc_info_rl)
    LinearLayout myAccInfoRl;
    @Bind(R.id.my_acc_repair_rl)
    LinearLayout myAccRepairRl;
    @Bind(R.id.my_acc_report_rl)
    LinearLayout myAccReportRl;
    @Bind(R.id.my_acc_coupons_rl)
    LinearLayout myAccCouponsRl;
    @Bind(R.id.my_acc_version_rl)
    LinearLayout myAccVersionRl;
    @Bind(R.id.my_acc_set)
    FrameLayout myAccSet;
    @Bind(R.id.mine_fragment)
    ViewPager viewPager;

    private View view;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = view = inflater.inflate(R.layout.main_fragment_mine, container, false);
        ButterKnife.bind(this, view);
        ArrayList<Drawable> arrayList = getDrawables();
        viewPager.setAdapter(new ImagePager(getContext(), arrayList));
        viewPager.setOnTouchListener(listener1);
        viewPager.setCurrentItem(viewPager.getAdapter().getCount() / 2);
        handler.sendEmptyMessageDelayed(SHOW_NEXT_PAGE, 2000);	// 3秒钟后显示下一页
        return view;
    }



    @NonNull
    private ArrayList<Drawable> getDrawables() {
        ArrayList<Drawable> arrayList = new ArrayList<>();
        arrayList.add(getResources().getDrawable(R.drawable.advera));
        arrayList.add(getResources().getDrawable(R.drawable.adverb));
        return arrayList;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @OnClick({R.id.my_acc_manager_rl, R.id.my_acc_info_rl, R.id.my_acc_repair_rl, R.id.my_acc_report_rl, R.id.my_acc_coupons_rl, R.id.my_acc_version_rl, R.id.my_acc_set})
    public void onClick(View view) {
        Intent it = new Intent();
        switch (view.getId()) {

            case R.id.my_acc_manager_rl:
                it.setClass(getContext(), VehicleManageActivity.class);
                break;
            case R.id.my_acc_info_rl:
                it.setClass(getContext(), VehicleInfoActivity.class);
                break;
            case R.id.my_acc_repair_rl:
                it.setClass(getContext(), RepairRecordActivity.class);
                break;
            case R.id.my_acc_report_rl:
                it.setClass(getContext(), ReportRecordActivity.class);
                break;
            case R.id.my_acc_coupons_rl:
                it.setClass(getContext(), ReportRecordActivity.class);
                break;
            case R.id.my_acc_version_rl:
                it.setClass(getContext(), VersionMsgActivity.class);
                break;
            case R.id.my_acc_set:
                it.setClass(getContext(), SetActivity.class);
                break;
        }
        startActivity(it);
    }

    /** 显示下一页 */
    private static final int SHOW_NEXT_PAGE = 0;

    private Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case SHOW_NEXT_PAGE:
                    showNextPage();
                    break;
            }
        }
    };
    /** 显示下一页 */
    public void showNextPage() {
        int currentItem = viewPager.getCurrentItem();	// 获取ViewPager当前显示的是哪一页
        viewPager.setCurrentItem(currentItem + 1);
        handler.sendEmptyMessageDelayed(SHOW_NEXT_PAGE, 2000);	// 3秒钟后显示下一页
    }


    float downX;
    float downY;
    ViewPager.OnTouchListener listener1 = new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    downX = event.getX();
                    downY = event.getY();
                    break;
                case MotionEvent.ACTION_UP:
                    float moveX = event.getX() - downX;
                    float moveY = event.getY() - downY;
                    downX = event.getX();
                    downY = event.getY();
                    if (Math.abs(moveX) < 10 && Math.abs(moveY) < 10) {
                        Intent intent = new Intent(getContext(), AccountGiftActivity.class);
                        startActivity(intent);
                        return  false;
                    }

            }
            return false;
        }
    };


    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(MyApplication.getContext());
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onResume(MyApplication.getContext());
    }

}
